<?php
include("include/conn.php");
require('include/Smarty/libs/Smarty.class.php');
$partyId        = isset($_POST['kaarigarIdEdit']) ? $_POST['kaarigarIdEdit'] : 0;
$msg = '';

if(isset($_POST['submit']))
{
	$partyId          = isset($_POST['party']) ? $_POST['party'] : 0;
  $kaarigarName     = isset($_REQUEST['kaarigarName']) ? $_REQUEST['kaarigarName'] : 0;
  $kaarigarAddress  = isset($_REQUEST['kaarigarAddress']) ? $_REQUEST['kaarigarAddress'] : 0;
  $kaarigarMobile   = isset($_REQUEST['kaarigarMobile']) ? $_REQUEST['kaarigarMobile'] : 0;
  $kaarigarFromDate = isset($_POST['toDateYear']) ? $_POST['toDateYear']."-".$_POST['toDateMonth']."-".$_POST['toDateDay'] : 0;
  $updateQueryKaarigar = "UPDATE kaarigar 
                             SET kaarigarname  = '".$kaarigarName."',
                                 kaarigarAddress  = '".$kaarigarAddress."',
                                 kaarigarMobile   = '".$kaarigarMobile."',
                                 kaarigarFromDate = '".$kaarigarFromDate."'
                           WHERE kaarigarid = '".$_POST['kaarigarIdEdit']."'";
  $updateQueryKaarigarRequest = mysql_query($updateQueryKaarigar);
  if(!$updateQueryKaarigarRequest)
  {
    die("Update Query Not Inserted : ".mysql_error());
  }
  else
  {
 		header("Location:kaarigarList.php");
  }
}
$kaarigarId        = '';
$kaarigarName      = '';
$kaarigarAddress   = '';
$kaarigarMobile    = '';

$selectQueryKaarigar = "SELECT kaarigarid,kaarigarname,kaarigarAddress,kaarigarMobile,kaarigarFromDate
                          FROM kaarigar
                         WHERE kaarigarid = ".$_REQUEST['kaarigarId'];
$selectQueryKaarigarResult = mysql_query($selectQueryKaarigar);
$selectQueryKaarigarNumRow = mysql_num_rows($selectQueryKaarigarResult);
if($selectQueryKaarigarNumRow > 0)
{
	while($kaarigarRow = mysql_fetch_array($selectQueryKaarigarResult))
	{
		$kaarigarId       = $kaarigarRow['kaarigarid'];
		$kaarigarName     = $kaarigarRow['kaarigarname'];
		$kaarigarAddress  = $kaarigarRow['kaarigarAddress'];
		$kaarigarMobile   = $kaarigarRow['kaarigarMobile'];
		$kaarigarFromDate = $kaarigarRow['kaarigarFromDate'];
	}
}
$total['totalAmt'] = 0;
$total['crAmount'] = 0;
$total['drAmount'] = 0;
// Selection Of Dr-Cr in kaarigar party :Start
if(isset($_REQUEST['kaarigarId']) && $_REQUEST['kaarigarId'] > 0)
{
 $toDate     = isset($_REQUEST['toDateYear']) ? $_REQUEST['toDateYear']."-".$_REQUEST['toDateMonth']."-".$_REQUEST['toDateDay'] : 0;
 $selectKaarigarDetail = "SELECT kaarigardetail.kaarigardetailId,kaarigardetail.kaarigarMasterId,kaarigardetail.particulars,kaarigardetail.debit,
                                 kaarigardetail.particular,kaarigardetail.credit,kaarigardetail.vadhaaroGhatado,kaarigardetail.addDebit,kaarigardetail.addCredit,
                                 kaarigardetail.addVadhaaroGhataado,kaarigardetail.qty,kaarigardetail.rate,kaarigardetail.crAmount,
                                 kaarigardetail.drAmount,kaarigarmaster.kaarigarMasterId,kaarigarmaster.kaarigarId,
                                 DATE_FORMAT(kaarigarmaster.kaarigarDate,'%Y-%m-%d') AS kaarigarMasterDate
                            FROM kaarigardetail
                            JOIN kaarigarmaster ON kaarigardetail.kaarigarMasterId = kaarigarmaster.kaarigarMasterId
                           WHERE kaarigarmaster.kaarigarDate <= '".$toDate."'
                             AND kaarigarmaster.kaarigarId = ".$_GET['kaarigarId']."
                           ORDER BY kaarigardetail.kaarigardetailId, kaarigardetail.kaarigarMasterId";
 $selectKaarigarDetailRes = mysql_query($selectKaarigarDetail);
 $b = 0;
 while($kaarigarDetailRow = mysql_fetch_assoc($selectKaarigarDetailRes))
 {
   $selectvadhaaroGhatado = "SELECT SUM(vadhaaroGhatado) AS addedvadhaaroGhatado
                               FROM kaarigardetail
                               JOIN kaarigarmaster
                              WHERE kaarigardetail.kaarigarMasterId = ".$kaarigarDetailRow['kaarigarMasterId']."
                           GROUP BY kaarigardetail.kaarigarMasterId";
   $selectvadhaaroGhatadoRes = mysql_query($selectvadhaaroGhatado);
   if($kaarigarAddDetailRow = mysql_fetch_assoc($selectvadhaaroGhatadoRes))
   {
     $kaarigarDetail[$b]['addedvadhaaroGhatado'] = $kaarigarAddDetailRow['addedvadhaaroGhatado'];
   }
     $kaarigarDetail[$b]['kaarigarId']          = $kaarigarDetailRow['kaarigarId'];
     $kaarigarDetail[$b]['kaarigardetailId']    = $kaarigarDetailRow['kaarigardetailId'];
     $kaarigarDetail[$b]['kaarigarMasterId']    = $kaarigarDetailRow['kaarigarMasterId'];
     $kaarigarDetail[$b]['kaarigarMasterDate']  = $kaarigarDetailRow['kaarigarMasterDate'];
     $kaarigarDetail[$b]['crAmount']            = $kaarigarDetailRow['crAmount'];
     $kaarigarDetail[$b]['drAmount']            = $kaarigarDetailRow['drAmount'];
     $kaarigarDetail[$b]['totalAmt']            = $kaarigarDetailRow['drAmount'] - $kaarigarDetailRow['crAmount'];
     $b++;
   
   $total['crAmount']    += $kaarigarDetailRow['crAmount'];
   $total['drAmount']    += $kaarigarDetailRow['drAmount'];
   $total['totalAmt']    += $kaarigarDetailRow['drAmount'] - $kaarigarDetailRow['crAmount'];
 }
} 
// Selection Of Dr-Cr in kaarigar party :End
	
if(isset($_POST['delete']) && $_POST['delete'] == 'Delete')
{
	
	//Delete Records Before INSERT Opening :Start
  $deleteQueryDetailKaarigar = " DELETE FROM kaarigardetail
                                WHERE kaarigarMasterId IN 
                                (SELECT kaarigarMasterId FROM kaarigarmaster WHERE kaarigarId = ".$partyId." AND kaarigarDate <= '".$toDate."')";
  $deleteQueryDetailKaarigarRes = mysql_query($deleteQueryDetailKaarigar);

  // Master Delete :Start
  $deleteQueryKaarigar   = "DELETE FROM kaarigarmaster
                            WHERE kaarigarId = ".$partyId."
                              AND kaarigarDate <= '".$toDate."'";
  $deleteQueryKaarigarRes  = mysql_query($deleteQueryKaarigar);
  // Master Delete :Start
}
		
$smarty = new Smarty;
require('bottom.php');
$smarty->assign("l_editKaarigar",$l_editKaarigar);
$smarty->assign("l_kaarigarName",$l_kaarigarName);

$smarty->assign("kaarigarId",$kaarigarId);
$smarty->assign("partyId",$partyId);
$smarty->assign("kaarigarName",$kaarigarName);
$smarty->assign("kaarigarAddress",$kaarigarAddress);
$smarty->assign("kaarigarMobile",$kaarigarMobile);
$smarty->assign("kaarigarFromDate",$kaarigarFromDate);
$smarty->assign("total",$total);
$smarty->display('kaarigarEdit.tpl');
?>
